﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO.Ports;
using System.Configuration;

namespace TY_Hoist.WinApp.Code
{
    /// <summary>
    /// 遥控器控制类
    /// </summary>
    public class RemoteControl : SerialPort
    {
        private List<byte> buffer = new List<byte>(4096);
        public RemoteControl()
        {

            this.PortName = ConfigurationManager.AppSettings["RCComNumber"].ToString();
            this.BaudRate = 9600;
            this.Parity = Parity.None;
            this.DataBits = 8;
            this.StopBits = StopBits.One;
            this.RtsEnable = true;
            this.ReadBufferSize = 1024;
            this.WriteBufferSize = 512;
            this.DtrEnable = true;
            this.Encoding = Encoding.GetEncoding("GB2312");

            this.Open();

        }



        /// <summary>
        /// 遥控器左
        /// </summary>
        public void RCLeft(bool swich)
        {
            if (swich)
            {
                this.CmdSend(RelayCmd.RELAY_8_ON);
            }
            else
            {
                this.CmdSend(RelayCmd.RELAY_8_OFF);
            }
        }

        /// <summary>
        /// 遥控器前
        /// </summary>
        public void RCUp(bool swich)
        {
            if (swich)
            {
                this.CmdSend(RelayCmd.RELAY_7_ON);
            }
            else
            {
                this.CmdSend(RelayCmd.RELAY_7_OFF);
            }
        }

        /// <summary>
        /// 遥控器右
        /// </summary>
        public void RCRight(bool swich)
        {
            if (swich)
            {
                this.CmdSend(RelayCmd.RELAY_6_ON);
            }
            else
            {
                this.CmdSend(RelayCmd.RELAY_6_OFF);
            }
        }

        /// <summary>
        /// 遥控器后
        /// </summary>
        public void RCDown(bool swich)
        {
            if (swich)
            {
                this.CmdSend(RelayCmd.RELAY_5_ON);
            }
            else
            {
                this.CmdSend(RelayCmd.RELAY_5_OFF);
            }
        }

        /// <summary>
        /// 遥控器下降
        /// </summary>
        public void RCBottom(bool swich)
        {
            if (swich)
            {
                this.CmdSend(RelayCmd.RELAY_4_ON);
            }
            else
            {
                this.CmdSend(RelayCmd.RELAY_4_OFF);
            }
        }

        /// <summary>
        /// 遥控器上升
        /// </summary>
        public void RCTop(bool swich)
        {
            if (swich)
            {
                this.CmdSend(RelayCmd.RELAY_3_ON);
            }
            else
            {
                this.CmdSend(RelayCmd.RELAY_3_OFF);
            }
        }


        /// <summary>
        /// 遥控器启动
        /// </summary>
        public void RCStart(bool swich)
        {
            if (swich)
            {
                this.CmdSend(RelayCmd.RELAY_2_ON);
            }
            else
            {
                this.CmdSend(RelayCmd.RELAY_2_OFF);
            }
        }

        /// <summary>
        /// 遥控器停止
        /// </summary>
        public void RCStop(bool swich)
        {
            if (swich)
            {
                this.CmdSend(RelayCmd.RELAY_1_ON);
            }
            else
            {
                this.CmdSend(RelayCmd.RELAY_1_OFF);
            }
        }

        /// <summary>
        /// 发送串口命令
        /// </summary>
        /// <param name="cmd"></param>
        public void CmdSend(byte[] cmd)
        {
            buffer = new List<byte>(4096);
            try
            {
                this.Write(cmd, 0, cmd.Length);
            }
            catch (Exception)
            {

                DSkin.Forms.DSkinMessageBox.Show("通信失败！");
                // throw;
            }

        }
        /// <summary>
        /// 清空缓存命令
        /// </summary>
        public void Clear()
        {
            this.DiscardInBuffer();
            this.DiscardOutBuffer();
            buffer.Clear();

        }
        /// <summary>
        /// 释放串口
        /// </summary>
        public new void Dispose()
        {
            if (this.IsOpen)
            {
                this.Close();
            }
            this.Dispose(true);
        }
    }
}
